#include "../inc/lcd.h"
#include "../inc/bmp.h"
//初始化液晶屏
int lcd_init()
{
    //1、打开液晶屏
    lcdFd = open("/dev/fb0",O_RDWR);
    if(lcdFd == -1)
    {
        perror("open lcd error");
        return -1;
    }
    //2、获取液晶屏设备的硬件参数
    if(ioctl(lcdFd, FBIOGET_VSCREENINFO, &varinfo) != 0)//获得可变属性
    {
        perror("获取LCD设备可变属性信息失败");
        return -1;
    }
    //800   480
    //printf("[可见区分辨率]: %d×%d\n", varinfo.xres, varinfo.yres);
    //printf("[虚拟区分辨率]: %d×%d\n", varinfo.xres_virtual, varinfo.yres_virtual);
    //printf("[色深]: %d bits\n", varinfo.bits_per_pixel); //32位   转成4个字节 32/8  
    // 获得当前显卡所支持的虚拟区显存大小
    unsigned long width  = varinfo.xres;
    unsigned long height = varinfo.yres;
    unsigned long bpp    = varinfo.bits_per_pixel;
    unsigned long screen_size = width * height * bpp/8;

    //3、进行内存映射
    //申请了虚拟区
    lcd_p = (int(*)[800])mmap(NULL,2*screen_size,PROT_READ|PROT_WRITE,MAP_SHARED,lcdFd,0);
    if(lcd_p == MAP_FAILED)
    {
        perror("mmap error");
        return -1;
    }

    return lcdFd;
    //int lcd_p[480][800]
}

//进入系统时的进度条
void time_prograss_bar(){
    for (int i = 0; i < 800; i++)
    {   
        for (int j = 410; j < 420; j++)
        {
            lcd_p[j][i] = GREEN;
            if(i == 200){
                //usleep(30000);
            }else if(i == 500){
                //usleep(30000);
            }else if(i == 790){
                //usleep(100000);
            }
        }
        usleep(1000);//10000
        if(i == 40){
            bmp_show_small(LONGMAO_SISTER_1,  40, 340);
        }else if(i == 200){
            bmp_show_small(LONGMAO_SISTER_2,  200, 340);
        }else if(i == 360){
            bmp_show_small(LONGMAO_SISTER_3,  360, 340);
        }else if(i == 520){
            bmp_show_small(LONGMAO_SISTER_4,  520, 340);
        }else if(i == 680){
            bmp_show_small(LONGMAO_SISTER_5,  680, 340);
        }
    }
}

//销毁液晶屏
int lcd_destory()
{
    //解除内存映射
    munmap(lcd_p, varinfo.xres*varinfo.yres*varinfo.bits_per_pixel/8);
    close(lcdFd);
}

// //初始化玩家角色液晶屏
// int lcd_player_init()
// {
//     //1、打开液晶屏
//     lcdFd_player = open("/dev/fb0",O_RDWR);
//     if(lcdFd_player == -1)
//     {
//         perror("open lcdFd_player error");
//         return -1;
//     }
//     //2、获取液晶屏设备的硬件参数
//     if(ioctl(lcdFd_player, FBIOGET_VSCREENINFO, &varinfo) != 0)
//     {
//         perror("获取LCD设备可变属性信息失败");
//         return -1;
//     }
//     //800   480
//     //printf("[可见区分辨率]: %d×%d\n", varinfo.xres, varinfo.yres);
//     //printf("[虚拟区分辨率]: %d×%d\n", varinfo.xres_virtual, varinfo.yres_virtual);
//     //printf("[色深]: %d bits\n", varinfo.bits_per_pixel); //32位   转成4个字节 32/8  
//     //3、进行内存映射
//     lcd_p_player = (int(*)[800])mmap(NULL,varinfo.xres*varinfo.yres*varinfo.bits_per_pixel/8,PROT_READ|PROT_WRITE,MAP_SHARED,lcdFd_player,0);
//     if(lcd_p_player == MAP_FAILED)
//     {
//         perror("mmap error");
//         return -1;
//     }

//     return lcdFd_player;
//     //int lcd_p[480][800]
// }

// //销毁玩家角色液晶屏
// int lcd_player_destory()
// {
//     //解除内存映射
//     munmap(lcd_p_player, varinfo.xres*varinfo.yres*varinfo.bits_per_pixel/8);
//     close(lcdFd_player);
// }




